/**
 * Created by Administrator on 2017/4/20.
 */


var Poker = require('../Poker/Poker');
var NN = require('../Poker/NN');

var PokerPlayer = require('../Poker/player').PokerPlayer;

var Const = require('./const');
var SubCode = require('../subCode');

exports.QZNNPlayer = PokerPlayer.extend({
    _robDealer: -1,             // 抢庄状态，-1/0/1 未选择/不抢/抢庄
    _selectedCards: null,     // 选择的三张牌
    _ante: 0,
    _playCards: false,
    _calcCards: null,
    _lookCards: false,
    _giveUp: false,

    init: function(uid, msg) {
        this._super(uid, msg);

        this._robDealer = -1;
        this._selectedCards = null;
        this._ante = 0;
        this._playCards = false;
        this._stat.roundPattern = Const.Pattern.NONE;
        this._calcCards = [];
        this._lookCards = false;
        this._giveUp = false;
    },

    setRotDealer: function(value) {
        this._robDealer = value;
    },

    getRobDealer: function() {
        return this._robDealer;
    },
    setAnte: function(value) {
        this._ante = value;
        var room = this.getRoom();
        room.broadcast("onPlayerAnteChange", {
            playerIndex: this.getIndex(),
            ante: this._ante
        });
    },

    getAnte: function() {
        return this._ante;
    },

    setSelectedCards: function(cardsArr) {
        if (!cardsArr || cardsArr.length == 0) {
            this._selectedCards = [];
            return true;
        }

        if(cardsArr.length != 3) {
            return false;
        }

        var cards = Poker.buildFromArray(cardsArr);
        var allExist = true;
        var allPoint = 0;

        ExObject.eachKeyNum(cards, function(card, num) {
            if (!card) {
                allExist = false;
            }

            if (this._handCards.getCardNum(card) < num) {
                allExist = false;
            }

            allPoint += NN.getPoint(card) * num;
        }.bind(this));

        if (!allExist) {
            return false;
        }

        if (allPoint % NN.BASE != 0) {
            return false;
        }

        this._selectedCards = [];
        cardsArr.forEach(function(card) {
            this._selectedCards.push(card);
        }.bind(this));

        return true;
    },

    isSelectedCards: function() {
        return (this._selectedCards) ? true : false;
    },
    /******************************************************************
     * 重载的接口
     ******************************************************************/

    getInfo: function() {
        var info = this._super();
        var room = this.getRoom();
        if(room.isSceneRoom()){
            info.remainScore = this._user.coin;
        }else {
            info.remainScore = this._stat.score;
        }

        return info;
    },

    getReconnectInfo: function(isSelf) {
        var info = this._super(isSelf);

        info.robDealer = this._robDealer;
        info.ante = this._ante;
        info.handCards = this._handCards.getCards();
        if (isSelf) {
            info.selectCards = this._selectedCards;
        }
        var room = this.getRoom();
        if(room.isSceneRoom()){
            info.remainScore = this._user.coin;
        }else {
            info.remainScore = this._stat.score;
        }

        info.giveUp = this._giveUp;
        info.playCards = this._playCards;
        info.lookCards = this._lookCards;
        info.selectNum = (this._selectedCards) ? this._selectedCards.length : -1;
        return info;
    },
    getInitHandCardNeedNum: function() {
        var room = this.getRoom();
        if(room.getOption("MS") == "KPQZ"){
            return 4;
        }else {
            return 5;
        }
    },
    getInitHandCardPreCards: function() {
        var preCards = [];
        if(this.getIndex() == 1){
            /*preCards.push(101);
            preCards.push(109);
            preCards.push(102);
            preCards.push(103);
            preCards.push(105);*/
        }else if(this.getIndex() == 2){
            /*preCards.push(201);
            preCards.push(209);
            preCards.push(202);
            preCards.push(203);
            preCards.push(205);*/
        }
        return preCards;
    },
    getSettlementInfo: function() {
        var info = this._super();

        info.handCards = this._handCards.getInitCards();
        info.selectCards = this._selectedCards;
        var room = this.getRoom();
        if(room.isSceneRoom()) {
            var config = room.getSceneConfig();
            var scoreRate = 1;
            if(config){
                scoreRate = config.scoreRate;
            }
            info.score = this._user.coin;
            info.roundScore *= scoreRate;
        }

        return info;
    },

    settlementPattern: function() {
        if (!this._selectedCards) {
            var results = NN.calcNiuCards(this._handCards.getInitCards());
            this._selectedCards = (results.length > 0) ? results[0] : [];
        }
        var allCardsArr = this._handCards.getInitCards();
        var pointCards = {};
        allCardsArr.forEach(function(card) {
            if(pointCards[Poker.getPoint(card)]){
                pointCards[Poker.getPoint(card)].num += 1;
                pointCards[Poker.getPoint(card)].cards.push(card);
            }else {
                pointCards[Poker.getPoint(card)] =  {
                    num: 0,
                    cards: []
                };
                pointCards[Poker.getPoint(card)].num = 1;
                pointCards[Poker.getPoint(card)].cards.push(card);
            }
        });
        var bomb = false;
        ExObject.eachKeyNum(pointCards, function(c, n){
            //console.log("card = %d  iiii = %j", c, n);
            if(n.num == 4){
                bomb = true;
                this._calcCards = n.cards;
                //console.log("index = %d, cards = %j", this.getIndex(), n.cards);
            }
        }.bind(this));
        if(bomb){
            this._stat.roundPattern = Const.Pattern.ZDN;
            return;
        }
        this._calcNiu();
    },

    settlement: function() {
        this._super();
        var room = this.getRoom();
        var player = this;
        var roomMS = room.getOption("MS");
        if (roomMS == "TBMS") {
            room.enumPlaying(function(eIndex, ePlayer) {
                if (eIndex == player.getIndex()) {
                    return;
                }
                var subConfig = room.getSubConfig();
                var di = subConfig.options.DI;
                var score = Const.PatternScore[player._stat.roundPattern] * di;
                //console.log("di = %d--------------", di);
                if (player._stat.roundPattern > ePlayer._stat.roundPattern) {
                    player.addRoundScore(score);
                    ePlayer.addRoundScore(-score);
                } else if (player._stat.roundPattern == ePlayer._stat.roundPattern) {
                    var maxCard = 0;
                    var playerMaxCard = 0;
                    if (player._stat.roundPattern == Const.Pattern.ZDN) {
                        maxCard = ePlayer._getMaxCard(ePlayer._calcCards);
                        //console.log("%d aaaaa %d", playerMaxCard, maxCard);

                        playerMaxCard = player._getMaxCard(player._calcCards);
                        //console.log("%d bbbbb %d", playerMaxCard, maxCard);

                    } else {
                        maxCard = ePlayer._getMaxCard();
                        playerMaxCard = player._getMaxCard();
                    }
                    console.log("%d cccccc %d  index = %d", playerMaxCard, maxCard, ePlayer.getIndex());
                    //console.log(" cardLT =  " + NN.cardLT(playerMaxCard, maxCard));

                    if (NN.cardLT(playerMaxCard, maxCard)) {
                        //var score = Const.PatternScore[player._stat.roundPattern] * player._ante;
                        ePlayer.addRoundScore(-score);
                        player.addRoundScore(score);
                    }
                }
            });

        } else if (roomMS == "FZZ" || roomMS == "KPQZ") {
            var ePlayer = this.getRoom().getDealerPlayer();
            if (!ePlayer) {
                return;
            }
            if (this.uid() == ePlayer.uid()) {
                return;
            }
            if (ePlayer._stat.roundPattern > player._stat.roundPattern) {
                var score = Const.PatternScore[ePlayer._stat.roundPattern] * player._ante;
                ePlayer.addRoundScore(score);
                player.addRoundScore(-score);
            } else if (ePlayer._stat.roundPattern < player._stat.roundPattern) {
                var score = Const.PatternScore[player._stat.roundPattern] * player._ante;
                player.addRoundScore(score);
                ePlayer.addRoundScore(-score);
            } else {
                var maxCard = 0;
                var playerMaxCard = 0;
                if(player._stat.roundPattern == Const.Pattern.ZDN){
                    maxCard = ePlayer._getMaxCard(ePlayer._calcCards);
                    //console.log("%d aaaaa %d", playerMaxCard, maxCard);

                    playerMaxCard = player._getMaxCard(player._calcCards);
                    //console.log("%d bbbbb %d", playerMaxCard, maxCard);

                }else {
                    maxCard = ePlayer._getMaxCard();
                    playerMaxCard = player._getMaxCard();
                }
                console.log("%d cccccc %d  index = %d", playerMaxCard, maxCard, ePlayer.getIndex());
                //console.log(" cardLT =  " + NN.cardLT(playerMaxCard, maxCard));
                if(Poker.getPoint(playerMaxCard) <= NN.BASE && player._stat.roundPattern != Const.Pattern.ZDN){
                    var score = Const.PatternScore[ePlayer._stat.roundPattern] * player._ante;
                    player.addRoundScore(-score);
                    ePlayer.addRoundScore(score);
                }else{
                    if (NN.cardLT(playerMaxCard, maxCard)){
                        var score = Const.PatternScore[player._stat.roundPattern] * player._ante;
                        ePlayer.addRoundScore(-score);
                        player.addRoundScore(score);
                    } else {
                        var score = Const.PatternScore[ePlayer._stat.roundPattern] * player._ante;
                        player.addRoundScore(-score);
                        ePlayer.addRoundScore(score);
                    }
                }
            }
        }else if(roomMS == "ZNMS"){
            if(player.getIndex() == room._roundWinPlayer){
                var score = 0;
                room.enumPlaying(function(eIndex, ePlayer){
                    var ante = ePlayer._ante;
                    ePlayer.addRoundScore(-ante);
                    score += ante;
                });
                player.addRoundScore(score);
            }
        }
    },

    onStartNewRound: function() {
        this._super();
        this._robDealer = -1;
        this._selectedCards = null;
        this._ante = 0;
        this._playCards = false;
        this._stat.roundPattern = Const.Pattern.NONE;
        this._calcCards = [];
        this._lookCards = false;
        this._giveUp = false;

    },

    /******************************************************************
     * 私有功能函数
     ******************************************************************/
    _calcNiu: function() {
        var selectCards = this._selectedCards;
        if (!selectCards || selectCards.length == 0) {
            this._stat.roundPattern = Const.Pattern.NONE;
            return;
        }

        var allCardsArr = this._handCards.getInitCards();
        var otherPoints = 0;
        allCardsArr.forEach(function(card) {
            if (selectCards.indexOf(card) == -1) {
                otherPoints += NN.getPoint(card);
            }
        });
        var nPoint = otherPoints % NN.BASE;
        var pattern = Const.Pattern.NONE;

        if (nPoint == 0) {
            pattern = Const.Pattern.NN;
            var minPoint = Poker.Point.MAX;
            var maxPoint = Poker.Point.MIN;
            allCardsArr.forEach(function(card) {
                var point = Poker.getPoint(card);
                minPoint = Math.min(minPoint, point);
                maxPoint = Math.max(maxPoint, point);
            });

            if (minPoint >= Poker.Point.J && maxPoint <= Poker.Point.K) {
                pattern = Const.Pattern.JIN;
            }
        } else {
            pattern = Const.Pattern['N' + nPoint];
        }

        this._stat.roundPattern = pattern;
    },

    _getMaxCard: function(cards) {
        var maxCard = 0;
        //var selectCards = this._selectedCards;
        var allCardsArr = this._handCards.getInitCards();
        if(cards){
            allCardsArr = cards;
        }
        allCardsArr.forEach(function(card) {
            //if (selectCards && selectCards.indexOf(card) == -1) {
            if (!maxCard || NN.cardLT(card, maxCard)) {
                maxCard = card;
            }
            // }
        });

        return maxCard;
    },
    _compar: function(index){
        var player = this;
        var room = player.getRoom();
        var ePlayer = room.getPlayerByIndex(index);
        if(player._stat.roundPattern == Const.Pattern.NONE){
            player.settlementPattern();
        }
        if(ePlayer._stat.roundPattern == Const.Pattern.NONE){
            ePlayer.settlementPattern();
        }
        if (player._stat.roundPattern > ePlayer._stat.roundPattern) {
            return true;
        } else if (player._stat.roundPattern == ePlayer._stat.roundPattern) {
            var maxCard = 0;
            var playerMaxCard = 0;
            if (player._stat.roundPattern == Const.Pattern.ZDN) {
                maxCard = ePlayer._getMaxCard(ePlayer._calcCards);
                playerMaxCard = player._getMaxCard(player._calcCards);
            } else {
                maxCard = ePlayer._getMaxCard();
                playerMaxCard = player._getMaxCard();
            }
            console.log("%d cccccc %d  index = %d", playerMaxCard, maxCard, ePlayer.getIndex());
            if (NN.cardLT(playerMaxCard, maxCard)) {
                return true;
            }
        }
        return false;
        //settlementPattern
    },
});